package com.ihr360.shared.encryption.convertor;

import com.alibaba.fastjson.JSON;
import com.ihr360.encryption.MysqlCipherInitializer;

import javax.persistence.Converter;

@Converter
public class JsonCryptoConverter extends AbstractCryptoConverter<Object> {

    public JsonCryptoConverter() {
        this(new MysqlCipherInitializer());
    }

    public JsonCryptoConverter(MysqlCipherInitializer cipherInitializer) {
        super(cipherInitializer);
    }


    @Override
    boolean isNotNullOrEmpty(Object attribute) {
        return attribute != null;
    }

    @Override
    Object stringToEntityAttribute(String dbData) {
        return JSON.parse(dbData);
    }

    @Override
    String entityAttributeToString(Object attribute) {
        return JSON.toJSON(attribute).toString();
    }
}